#ifndef TIMER_MANAGER_H
#define TIMER_MANAGER_H

#include <iostream>
#include <atomic>
#include <vector>
#include <thread>
#include <memory>
#include <chrono>
#include "task_interface.h"

class TimerManager {
private:
    std::atomic<bool> m_running;
    std::thread m_timerThread;
    std::chrono::seconds m_interval;
    std::function<void()> m_callback;  // 回调函数
    // 其他成员...
    
public:
    explicit TimerManager(std::chrono::seconds interval) 
        : m_running(false), m_interval(interval) {}
    
    // 注册回调函数（替代原有的registerTask）
    void registerCallback(std::function<void()> callback) {
        m_callback = callback;
    }
    
    // 启动定时器
    void start() {
        if (m_running) return;
        m_running = true;
        m_timerThread = std::thread([this]() {
            while (m_running) {
                std::this_thread::sleep_for(m_interval);
                if (m_running && m_callback) {
                    m_callback();  // 执行回调，回调中会将任务加入线程池
                }
            }
        });
    }
    
    // 停止定时器
    void stop() {
        if (!m_running) return;
        m_running = false;
        if (m_timerThread.joinable())
            m_timerThread.join();
    }
};

#endif